// Copyright 2017 Baidu Inc. All Rights Reserved.
// Author: Duan Lian (duanlian01@baidu.com)
//
// Description: OTA Coap Client

#include "baidu_iot_ota_coap_client.h"

namespace duer {

bca_status_t coaps_callback(bca_context, bca_coap_handler, const bca_msg_t*, const bca_addr_t*) {
    return 0;
}

CoapsClient::CoapsClient(const char* ip, unsigned short port, const char* path, const char* cert) :
        _on_network_data_signal(this, &CoapsClient::network_signal_handler),
        _m_b_connected(false),
        _m_coap_instance(NULL) {
        _m_coap_instance = bca_coap_acquire(coaps_callback, this,
                                      &_on_network_data_signal);
    _m_peer.host = (void*)ip;
    _m_peer.port = port;
    _m_peer.type = BCA_PROTO_UDP;
    _m_szpath    = path;
    _m_szcert    = cert;
    _m_peer.host_size = strlen(ip);
}

CoapsClient::~CoapsClient() {
}

int CoapsClient::get(unsigned int nOffset, unsigned char chBuffer[], unsigned int nSize) {
    if (!_m_b_connected) {
        connect();
    }

    return 0;
}

int CoapsClient::connect() {
    bca_coap_connect(_m_coap_instance, &_m_peer, _m_szcert, strlen(_m_szcert));
    _m_b_connected = true;

    return 0;
}

int CoapsClient::disconnect() {
    bca_coap_disconnect(_m_coap_instance);
    _m_b_connected = false;

    return 0;
}

void CoapsClient::network_signal_handler(const bca_addr_t* addr) {
    return;
}

} // namespace duer
